package tcu.iot.gasmanage.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.entity.Options;
import tcu.iot.gasmanage.entity.Value;
import tcu.iot.gasmanage.entity.equipment.EquipmentData;
import tcu.iot.gasmanage.entity.home.SiteQuery;
import tcu.iot.gasmanage.entity.home.SiteReturn;
import tcu.iot.gasmanage.mapper.EquipmentMapper;
import tcu.iot.gasmanage.mapper.HomeMapper;
import tcu.iot.gasmanage.mapper.PersonMapper;
import tcu.iot.gasmanage.mapper.TDengineMapper;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class HomeService {

    @Resource
    private HomeMapper homeMapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private EquipmentMapper equipmentMapper;

    static HashMap<String, Object> hashMap = new HashMap<>();

    public Result getStateData() {
        hashMap.put("stateData", homeMapper.GetStateData());

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result getProvinceData() {
        hashMap.put("regionData", homeMapper.GetProvinceData());

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result getHomeBarChartData(SiteQuery site) {
        List<String> equipmentList = new ArrayList<>();
        List<String> signalList = new ArrayList<>();
        List<String> allSignalList = equipmentMapper.GetEquipmentWithSignal();

        for (int i = 0; i < site.getSelectEquipment().size(); i++) {
            equipmentList.add(site.getSelectEquipment().get(i).get(1));
        }

        for (int i = 0; i < equipmentList.size(); i++) {
            for (String s : allSignalList) {
                if (Objects.equals(equipmentList.get(i), s)) {
                    signalList.add(equipmentList.get(i));
                    equipmentList.remove(equipmentList.get(i));
                }
            }
        }

        List<EquipmentData> equipmentInfoResults = TDengineMapper.HomeBarChartData(equipmentList, signalList);

        List<List<Object>> homeBarDataList = new ArrayList<>();

        for (EquipmentData equipmentInfoResult : equipmentInfoResults) {
            List<Object> HomeBarData = new ArrayList<>();

            String equipmentName = homeMapper.getEquipmentName(equipmentInfoResult.getId());
            HomeBarData.add(equipmentName);
            HomeBarData.add(equipmentInfoResult.getI1());
            HomeBarData.add(equipmentInfoResult.getI2());
            HomeBarData.add(equipmentInfoResult.getI3());
            HomeBarData.add(equipmentInfoResult.getI4());
            homeBarDataList.add(HomeBarData);
        }

        hashMap.put("source", homeBarDataList);

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result getSiteOptions() {
        //HashMap<String, Object> hashMap = new HashMap<>();
        List<SiteReturn> siteReturnList = new ArrayList<>();

        List<Options> siteList = personMapper.FindSiteOptions();

        for (Options options : siteList) {
            List<Value> equipmentList = homeMapper.GetSiteEquipment(options.getKey());

            SiteReturn siteReturn = new SiteReturn(options.getKey(), options.getLabel(), equipmentList);

            siteReturnList.add(siteReturn);

        }

        hashMap.put("siteOptions", siteReturnList);

        return Result.ok("true", 200, "成功", hashMap);
    }
}
